const profileService = require('../service/profile.service');

class ProfileController {

  // 创建用户资料
  async create(ctx, next) {
    // 1.获取参数
    const {gender, phone, qq, wx, email, suggest, website, address, emotion, birth, name} = ctx.request.body;
    const {id} = ctx.user;
    // 2.操作数据库
    const result = await profileService.create(id, gender, phone, qq, wx, email, suggest, website, address, emotion, birth, name);
    // 3.返回结果
    ctx.body = result;
  }

  // 获取用户资料
  async profile(ctx, next) {
    // 1.获取参数
    const {userId} = ctx.query;
    // 2.操作数据库
    const result = await profileService.profile(userId);
    // 3.返回结果
    ctx.body = result;
  }

  // 修改用户资料
  async update(ctx, next) {
    // 1.获取参数
    const {gender, phone, qq, wx, email, suggest, website, address, emotion, birth, name} = ctx.request.body;
    const {id} = ctx.user;
    // 2.修改资料
    const result = await profileService.update(id, gender, phone, qq, wx, email, suggest, website, address, emotion, birth, name);
    // 3.返回结果
    ctx.body = result;
  }

  // 验证用户是否有资料
  async isExistProfile(ctx, next) {
    // 1.获取参数
    const {id} = ctx.user;
    // 2.操作数据库
    const result = await profileService.isExistProfile(id);
    // 3.返回结果
    ctx.body = result.length > 0 ? true : false;
  }

}

module.exports = new ProfileController();
